﻿using System;
using System.Linq;
using System.Collections.Generic;
using Sigil.Impl;

namespace Sigil
{
	/// <summary>
	/// Provides a way to lookup locals in scope by name.
	/// </summary>
	public class LocalLookup
	{
		/// <summary>
		/// Returns the local with the given name.
		///
		/// Throws KeyNotFoundException if no local by that name is found".
		/// </summary>
		public Local this[String name]
		{
			get
			{
				if (!InnerLookup.ContainsKey(name))
				{
					throw new KeyNotFoundException("No local with name '" + name + "' found");
				}

				return InnerLookup[name];
			}
		}

		/// <summary>
		/// Returns the number of locals in scope
		/// </summary>
		public Int32 Count { get { return _Names.Count(); } }

		private IEnumerable<String> _Names { get { return InnerLookup.Keys.Where(k => !k.StartsWith("__")).ToList(); } }

		/// <summary>
		/// Returns the names of all the locals in scope
		/// </summary>
		public IEnumerable<String> Names { get { return _Names; } }

		private Dictionary<String, Local> InnerLookup;

		internal LocalLookup(Dictionary<String, Local> innerLookup)
		{
			InnerLookup = innerLookup;
		}
	}
}